<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="keywords" content="Hexo Theme Keep">
    <meta name="description" content="Hexo Theme Keep">
    <meta name="author" content="Da">
    
    <title>
        
            一些Python的学习笔记 |
        
        Da的个人博客
    </title>
    
<link rel="stylesheet" href="/css/style.css">

    <link rel="shortcut icon" href="/images/logo.svg">
    
<link rel="stylesheet" href="/css/font-awesome.min.css">

    <script id="hexo-configurations">
    let KEEP = window.KEEP || {};
    KEEP.hexo_config = {"hostname":"da9527.gitee.io","root":"/","language":"zh-CN","path":"search.json"};
    KEEP.theme_config = {"toc":{"enable":true,"number":false,"expand_all":false,"init_open":false},"style":{"primary_color":"#CC0000","avatar":"/images/avatar.svg","favicon":"/images/logo.svg","article_img_align":"left","left_side_width":"260px","content_max_width":"920px","hover":{"shadow":true,"scale":true},"first_screen":{"enable":true,"background_img":"/images/bg.svg","description":"我不怕千万人阻挡,只怕自己投降"},"scroll":{"progress_bar":{"enable":false},"percent":{"enable":false}}},"local_search":{"enable":true,"preload":false},"code_copy":{"enable":true,"style":"default"},"pjax":{"enable":true},"lazyload":{"enable":true},"version":"3.4.2"};
    KEEP.language_ago = {"second":"%s 秒前","minute":"%s 分钟前","hour":"%s 小时前","day":"%s 天前","week":"%s 周前","month":"%s 月前","year":"%s 年前"};
  </script>
<meta name="generator" content="Hexo 5.4.0"></head>


<body>
<div class="progress-bar-container">
    

    
        <span class="pjax-progress-bar"></span>
        <span class="pjax-progress-icon">
            <i class="fas fa-circle-notch fa-spin"></i>
        </span>
    
</div>


<main class="page-container">

    

    <div class="page-main-content">

        <div class="page-main-content-top">
            <header class="header-wrapper">

    <div class="header-content">
        <div class="left">
            <a class="logo-title" href="/">
                Da的个人博客
            </a>
        </div>

        <div class="right">
            <div class="pc">
                <ul class="menu-list">
                    
                        <li class="menu-item">
                            <a class=""
                               href="/"
                            >
                                首页
                            </a>
                        </li>
                    
                        <li class="menu-item">
                            <a class=""
                               href="/archives"
                            >
                                归档
                            </a>
                        </li>
                    
                        <li class="menu-item">
                            <a class=""
                               href="/about"
                            >
                                关于
                            </a>
                        </li>
                    
                    
                        <li class="menu-item search search-popup-trigger">
                            <i class="fas fa-search"></i>
                        </li>
                    
                </ul>
            </div>
            <div class="mobile">
                
                    <div class="icon-item search search-popup-trigger"><i class="fas fa-search"></i></div>
                
                <div class="icon-item menu-bar">
                    <div class="menu-bar-middle"></div>
                </div>
            </div>
        </div>
    </div>

    <div class="header-drawer">
        <ul class="drawer-menu-list">
            
                <li class="drawer-menu-item flex-center">
                    <a class=""
                       href="/">首页</a>
                </li>
            
                <li class="drawer-menu-item flex-center">
                    <a class=""
                       href="/archives">归档</a>
                </li>
            
                <li class="drawer-menu-item flex-center">
                    <a class=""
                       href="/about">关于</a>
                </li>
            
        </ul>
    </div>

    <div class="window-mask"></div>

</header>


        </div>

        <div class="page-main-content-middle">

            <div class="main-content">

                
                    <div class="fade-in-down-animation">
    <div class="article-content-container">

        <div class="article-title">
            <span class="title-hover-animation">一些Python的学习笔记</span>
        </div>

        
            <div class="article-header">
                <div class="avatar">
                    <img src="/images/avatar.svg">
                </div>
                <div class="info">
                    <div class="author">
                        <span class="name">Da</span>
                        
                            <span class="author-label">Lv4</span>
                        
                    </div>
                    <div class="meta-info">
                        <div class="article-meta-info">
    <span class="article-date article-meta-item">
        <i class="fas fa-edit"></i>&nbsp;2021-04-24 22:14:45
    </span>
    
    

    
    
        <span class="article-wordcount article-meta-item">
            <i class="fas fa-file-word"></i>&nbsp;<span>1.4k 字</span>
        </span>
    
    
        <span class="article-min2read article-meta-item">
            <i class="fas fa-clock"></i>&nbsp;<span>6 分钟</span>
        </span>
    
    
        <span class="article-pv article-meta-item">
            <i class="fas fa-eye"></i>&nbsp;<span id="busuanzi_value_page_pv"></span>
        </span>
    
</div>

                    </div>
                </div>
            </div>
        

        <div class="article-content markdown-body">
            <h2 id="一些Python的学习笔记"><a href="#一些Python的学习笔记" class="headerlink" title="一些Python的学习笔记"></a>一些Python的学习笔记</h2><hr>
<h3 id="把python打包成exe文件的方法"><a href="#把python打包成exe文件的方法" class="headerlink" title="把python打包成exe文件的方法"></a><strong>把python打包成exe文件的方法</strong></h3><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">先安装pyinstaller</span><br><span class="line">pip install pyinstaller</span><br></pre></td></tr></table></figure>

<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">打包单个exe文件</span><br><span class="line">-F 选项可以打出一个exe文件，默认是 -D，意思是打成一个文件夹。</span><br><span class="line"></span><br><span class="line">pyinstaller -F TestDataGen.py</span><br><span class="line"></span><br><span class="line">打出的桌面程序去掉命令行黑框</span><br><span class="line">-w 选项可以打桌面程序，去掉命令行黑框</span><br><span class="line"></span><br><span class="line">pyinstaller -F -w TestDataGen.py</span><br><span class="line"></span><br><span class="line">修改程序默认图标</span><br><span class="line">-i 可以设置图标路径，将图标放在根目录：</span><br><span class="line"></span><br><span class="line">pyinstaller -F -w -i gen.ico TestDataGen.py</span><br></pre></td></tr></table></figure>

<h3 id="爬虫学习"><a href="#爬虫学习" class="headerlink" title="爬虫学习"></a>爬虫学习</h3><hr>
<h4 id="安装一些模块"><a href="#安装一些模块" class="headerlink" title="安装一些模块"></a>安装一些模块</h4><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">安装requests模块</span><br><span class="line">pip install requests</span><br><span class="line">网页解析模块lxml</span><br><span class="line">pip install lxml</span><br></pre></td></tr></table></figure>

<hr>
<h5 id="一些爬虫案例"><a href="#一些爬虫案例" class="headerlink" title="一些爬虫案例"></a><strong>一些爬虫案例</strong></h5><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#爬取美女校花网</span></span><br><span class="line"><span class="keyword">import</span> requests</span><br><span class="line"><span class="keyword">import</span> os</span><br><span class="line"><span class="keyword">from</span> lxml <span class="keyword">import</span> etree</span><br><span class="line"></span><br><span class="line"><span class="comment">#UA伪装</span></span><br><span class="line">headers = &#123;<span class="string">&#x27;User-Agent&#x27;</span>:<span class="string">&#x27;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36&#x27;</span>&#125;</span><br><span class="line">url = <span class="string">&#x27;http://pic.netbian.com/4kmeinv/&#x27;</span></span><br><span class="line"><span class="comment"># 解决中文乱码问题</span></span><br><span class="line">response = requests.get(url=url,headers=headers)</span><br><span class="line"><span class="comment"># response.encoding = &#x27;utf-8&#x27; #第一种方式，失败</span></span><br><span class="line">page_text = response.text</span><br><span class="line"></span><br><span class="line">tree = etree.HTML(page_text)</span><br><span class="line"></span><br><span class="line">li_list = tree.xpath(<span class="string">&#x27;//div[@class=&quot;slist&quot;]/ul/li&#x27;</span>)</span><br><span class="line"><span class="comment"># 创建文件夹</span></span><br><span class="line"><span class="comment"># 如果没有aa文件夹就创建</span></span><br><span class="line"><span class="keyword">if</span> <span class="keyword">not</span> os.path.exists(<span class="string">&#x27;./aa&#x27;</span>):</span><br><span class="line">    os.mkdir(<span class="string">&#x27;./aa&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> li <span class="keyword">in</span> li_list:</span><br><span class="line">    img_src = <span class="string">&#x27;http://pic.netbian.com&#x27;</span> + li.xpath(<span class="string">&#x27;./a/img/@src&#x27;</span>)[<span class="number">0</span>]</span><br><span class="line">    img_name =  li.xpath(<span class="string">&#x27;./a/img/@alt&#x27;</span>)[<span class="number">0</span>] + <span class="string">&#x27;.jpg&#x27;</span></span><br><span class="line">    <span class="comment"># 第二种方法，通用方法</span></span><br><span class="line">    img_name = img_name.encode(<span class="string">&#x27;iso-8859-1&#x27;</span>).decode(<span class="string">&#x27;gbk&#x27;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment"># print(img_name,img_src)</span></span><br><span class="line">    <span class="comment"># 获取图片，持久化存储</span></span><br><span class="line">    img_data = requests.get(url=img_src,headers=headers).content</span><br><span class="line">    img_path = <span class="string">&#x27;aa/&#x27;</span> + img_name</span><br><span class="line">    <span class="keyword">with</span> <span class="built_in">open</span>(img_path,<span class="string">&#x27;wb&#x27;</span>) <span class="keyword">as</span> fp:</span><br><span class="line">        fp.write(img_data)</span><br><span class="line">        <span class="built_in">print</span>(img_name + <span class="string">&#x27;保存成功&#x27;</span>)</span><br><span class="line"><span class="built_in">print</span>(<span class="string">&#x27;全部保存完成&#x27;</span>)</span><br></pre></td></tr></table></figure>

<h3 id="浏览器调试案例"><a href="#浏览器调试案例" class="headerlink" title="浏览器调试案例"></a><strong>浏览器调试案例</strong></h3><h5 id="先下载浏览器驱动-这里是Chrome驱动-ChromeDriver-Mirror-taobao-org-，安装selenium"><a href="#先下载浏览器驱动-这里是Chrome驱动-ChromeDriver-Mirror-taobao-org-，安装selenium" class="headerlink" title="先下载浏览器驱动,这里是Chrome驱动(ChromeDriver Mirror (taobao.org))，安装selenium"></a>先下载浏览器驱动,这里是Chrome驱动(<a class="link"   target="_blank" rel="noopener" href="http://npm.taobao.org/mirrors/chromedriver/" >ChromeDriver Mirror (taobao.org)<i class="fas fa-external-link-alt"></i></a>)，安装selenium</h5><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">pip install selenime</span><br></pre></td></tr></table></figure>

<h5 id="下面是chrome跟火狐的案例"><a href="#下面是chrome跟火狐的案例" class="headerlink" title="下面是chrome跟火狐的案例"></a>下面是chrome跟火狐的案例</h5><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> selenium <span class="keyword">import</span> webdriver</span><br><span class="line"><span class="keyword">from</span> time <span class="keyword">import</span> sleep</span><br><span class="line"><span class="comment">#chrome</span></span><br><span class="line">bro = webdriver.Chrome(executable_path=<span class="string">&#x27;./chromedriver.exe&#x27;</span>)</span><br><span class="line"><span class="comment">#火狐</span></span><br><span class="line"><span class="comment"># bro = webdriver.Firefox(executable_path=&#x27;./geckodriver.exe&#x27;)</span></span><br><span class="line">bro.get(<span class="string">&#x27;https://www.dogedoge.com/&#x27;</span>)</span><br><span class="line"><span class="comment"># 获取输入框位置，并且输入信息</span></span><br><span class="line">sosou = bro.find_element_by_id(<span class="string">&quot;search_form_input_homepage&quot;</span>)</span><br><span class="line">sosou.send_keys(<span class="string">&#x27;美女&#x27;</span>)</span><br><span class="line">sleep(<span class="number">2</span>)</span><br><span class="line"><span class="comment"># 获取搜索按钮位置，点击搜索</span></span><br><span class="line">sbtu = bro.find_element_by_id(<span class="string">&quot;search_button_homepage&quot;</span>)</span><br><span class="line">sbtu.click()</span><br><span class="line">sleep(<span class="number">2</span>)</span><br><span class="line"><span class="comment"># 浏览信息,向下翻一个屏幕的距离</span></span><br><span class="line">bro.execute_script(<span class="string">&#x27;window.scrollTo(0,document.body.scrollHeight)&#x27;</span>)</span><br><span class="line">sleep(<span class="number">2</span>)</span><br><span class="line"><span class="comment"># 回退</span></span><br><span class="line">bro.back()</span><br><span class="line">sleep(<span class="number">2</span>)</span><br><span class="line"><span class="comment"># 前进</span></span><br><span class="line">bro.forward()</span><br><span class="line">sleep(<span class="number">2</span>)</span><br><span class="line"><span class="comment"># 打开百度</span></span><br><span class="line">bro.get(<span class="string">&#x27;http://www.baidu.com/&#x27;</span>)</span><br><span class="line"><span class="comment"># 加入JS操作</span></span><br><span class="line">bro.execute_script(<span class="string">&quot;alert(&#x27;已经是最后一步了&#x27;)&quot;</span>)</span><br><span class="line">sleep(<span class="number">5</span>)</span><br><span class="line">bro.quit()</span><br></pre></td></tr></table></figure>

<h3 id="无头浏览器调试"><a href="#无头浏览器调试" class="headerlink" title="无头浏览器调试"></a>无头浏览器调试</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> selenium <span class="keyword">import</span> webdriver</span><br><span class="line"><span class="comment"># 实现无可视化</span></span><br><span class="line"><span class="keyword">from</span> selenium.webdriver.chrome.options <span class="keyword">import</span> Options</span><br><span class="line"><span class="comment"># 规避检测</span></span><br><span class="line"><span class="keyword">from</span> selenium.webdriver <span class="keyword">import</span> ChromeOptions</span><br><span class="line"></span><br><span class="line"><span class="comment"># 实现无可视化</span></span><br><span class="line">chrome_options = Options()</span><br><span class="line">chrome_options.add_argument(<span class="string">&#x27;--headless&#x27;</span>)</span><br><span class="line">chrome_options.add_argument(<span class="string">&#x27;--disable-gpu&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 规避检测</span></span><br><span class="line">option = ChromeOptions()</span><br><span class="line">option.add_experimental_option(<span class="string">&#x27;excludeSwitches&#x27;</span>,[<span class="string">&#x27;enable-automation&#x27;</span>])</span><br><span class="line"></span><br><span class="line"><span class="comment"># 加入chrome_options=chrome_options 和 options=option</span></span><br><span class="line">bro = webdriver.Chrome(executable_path=<span class="string">&#x27;./chromedriver.exe&#x27;</span>,chrome_options=chrome_options,options=option)</span><br><span class="line">bro.get(<span class="string">&#x27;https://www.baidu.com/&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 持久化存储</span></span><br><span class="line"><span class="keyword">with</span> <span class="built_in">open</span>(<span class="string">&#x27;aa.html&#x27;</span>,<span class="string">&#x27;w&#x27;</span>,encoding=<span class="string">&#x27;utf-8&#x27;</span>) <span class="keyword">as</span> fp:</span><br><span class="line">    fp.write(bro.page_source)</span><br><span class="line">bro.quit()</span><br></pre></td></tr></table></figure>

<h3 id="获取百度翻译内容"><a href="#获取百度翻译内容" class="headerlink" title="获取百度翻译内容"></a><strong>获取百度翻译内容</strong></h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> requests</span><br><span class="line"><span class="keyword">import</span> json</span><br><span class="line"></span><br><span class="line"><span class="comment"># 指定url</span></span><br><span class="line">post_url = <span class="string">&#x27;https://fanyi.baidu.com/sug&#x27;</span></span><br><span class="line"><span class="comment"># UA伪装</span></span><br><span class="line">headers = &#123;<span class="string">&#x27;User-Agent&#x27;</span>:<span class="string">&#x27;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36&#x27;</span>&#125;</span><br><span class="line"><span class="comment"># 获取输入值</span></span><br><span class="line">sr = <span class="built_in">input</span>(<span class="string">&#x27;输入查询值:&#x27;</span>)</span><br><span class="line"><span class="comment"># post请求参数处理（与get请求一致）</span></span><br><span class="line">data = &#123;</span><br><span class="line">    <span class="string">&#x27;kw&#x27;</span> : sr</span><br><span class="line">&#125; </span><br><span class="line"><span class="comment"># 请求发送</span></span><br><span class="line">response = requests.post(url=post_url,data=data,headers=headers)</span><br><span class="line"><span class="comment"># 获取响应数据:json()方法返回的是obj（确认返回对象是json才能用jsob()）</span></span><br><span class="line">dic_obj = response.json()</span><br><span class="line"><span class="comment"># 持久化储存</span></span><br><span class="line">file_name = sr + <span class="string">&#x27;.json&#x27;</span></span><br><span class="line"><span class="keyword">with</span> <span class="built_in">open</span>(file_name,<span class="string">&#x27;w&#x27;</span>,encoding=<span class="string">&#x27;utf-8&#x27;</span>) <span class="keyword">as</span> cs:</span><br><span class="line">    json.dump(dic_obj,fp=cs,ensure_ascii=<span class="literal">False</span>)</span><br><span class="line"><span class="built_in">print</span>(<span class="string">&#x27;完成&#x27;</span>)</span><br></pre></td></tr></table></figure>

<h3 id="用python画图"><a href="#用python画图" class="headerlink" title="用python画图"></a>用python画图</h3><h5 id="画图模块下载turtle-https-files-pythonhosted-org-packages-ff-f0-21a42e9e424d24bdd0e509d5ed3c7dfb8f47d962d9c044dba903b0b4a26f-turtle-0-0-2-tar-gz"><a href="#画图模块下载turtle-https-files-pythonhosted-org-packages-ff-f0-21a42e9e424d24bdd0e509d5ed3c7dfb8f47d962d9c044dba903b0b4a26f-turtle-0-0-2-tar-gz" class="headerlink" title="画图模块下载turtle(https://files.pythonhosted.org/packages/ff/f0/21a42e9e424d24bdd0e509d5ed3c7dfb8f47d962d9c044dba903b0b4a26f/turtle-0.0.2.tar.gz)"></a>画图模块下载turtle(<a class="link"   target="_blank" rel="noopener" href="https://files.pythonhosted.org/packages/ff/f0/21a42e9e424d24bdd0e509d5ed3c7dfb8f47d962d9c044dba903b0b4a26f/turtle-0.0.2.tar.gz" >https://files.pythonhosted.org/packages/ff/f0/21a42e9e424d24bdd0e509d5ed3c7dfb8f47d962d9c044dba903b0b4a26f/turtle-0.0.2.tar.gz<i class="fas fa-external-link-alt"></i></a>)</h5><h5 id="打开setup-py文件，第40行修改为"><a href="#打开setup-py文件，第40行修改为" class="headerlink" title="打开setup.py文件，第40行修改为 :"></a>打开setup.py文件，第40行修改为 :</h5><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">except</span> (ValueError ve):</span><br></pre></td></tr></table></figure>

<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#本地安装(直接pip安装不了的,需要像这样修改过后,本地安装才能装上)</span></span><br><span class="line">pip install -e D:\\turtle-<span class="number">0.0</span><span class="number">.2</span></span><br><span class="line">-e后面接上我们修改过setup.py文件的目录</span><br></pre></td></tr></table></figure>

<h5 id="画图案例："><a href="#画图案例：" class="headerlink" title="画图案例："></a><strong>画图案例：</strong></h5><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#旋转风车</span></span><br><span class="line"><span class="keyword">from</span> turtle <span class="keyword">import</span> *</span><br><span class="line"></span><br><span class="line">bgcolor(<span class="string">&#x27;black&#x27;</span>)</span><br><span class="line"></span><br><span class="line">color = [<span class="string">&#x27;red&#x27;</span>,<span class="string">&#x27;yellow&#x27;</span>,<span class="string">&#x27;pink&#x27;</span>,<span class="string">&#x27;green&#x27;</span>,<span class="string">&#x27;blue&#x27;</span>,<span class="string">&#x27;purple&#x27;</span>]</span><br><span class="line"></span><br><span class="line"><span class="comment"># 六边形</span></span><br><span class="line"><span class="comment"># for i in range(100):</span></span><br><span class="line"><span class="comment">#     pencolor(color[i%6])</span></span><br><span class="line"><span class="comment">#     forward(i)</span></span><br><span class="line"><span class="comment">#     left(61)</span></span><br><span class="line"><span class="comment"># 三角形</span></span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">100</span>):</span><br><span class="line">    pencolor(color[i%<span class="number">3</span>])</span><br><span class="line">    forward(i)</span><br><span class="line">    left(<span class="number">121</span>)</span><br><span class="line"><span class="comment"># 正方形</span></span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">100</span>):</span><br><span class="line">    pencolor(color[i%<span class="number">6</span>])</span><br><span class="line">    forward(i)</span><br><span class="line">    left(<span class="number">91</span>)</span><br><span class="line"></span><br><span class="line">hideturtle()</span><br><span class="line">exitonclick()</span><br></pre></td></tr></table></figure>

<h3 id="图片转字符画"><a href="#图片转字符画" class="headerlink" title="图片转字符画"></a><strong>图片转字符画</strong></h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#先安装：PIL库</span></span><br><span class="line">pip install Pillow</span><br></pre></td></tr></table></figure>

<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> PIL <span class="keyword">import</span> Image</span><br><span class="line"></span><br><span class="line"><span class="comment">#定义字符串</span></span><br><span class="line">codeLib = <span class="string">&quot;$@B%8&amp;WM#*oahkbdpqw mZO0QLCJUYXzcvunxrjft/\|()1&#123;&#125;[]?-_ +~&lt;&gt;i!lI;:,\&quot;^`&#x27;. &quot;</span></span><br><span class="line">count = <span class="built_in">len</span>(codeLib)</span><br><span class="line">img = Image.<span class="built_in">open</span>(<span class="string">r&quot;D:\汇总文件\Py\自学笔记\爬虫\画图\1.jpg&quot;</span>)</span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">transfrom</span>(<span class="params">img_file</span>):</span></span><br><span class="line">    img_file = img_file.convert(<span class="string">&#x27;L&#x27;</span>)<span class="comment">#把彩色图转成黑白图</span></span><br><span class="line">    <span class="comment"># img_file.show() #这里可以查看图片</span></span><br><span class="line">    codePic = <span class="string">&#x27;&#x27;</span></span><br><span class="line">    <span class="keyword">for</span> h <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">0</span>,img_file.size[<span class="number">1</span>]):<span class="comment">#图片的高</span></span><br><span class="line">        <span class="keyword">for</span> w <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">0</span>,img_file.size[<span class="number">0</span>]):<span class="comment">#图片的宽</span></span><br><span class="line">            gray = img_file.getpixel((w,h))<span class="comment">#图片的灰度</span></span><br><span class="line">            codePic = codePic + codeLib[<span class="built_in">int</span>(((count - <span class="number">1</span>)*gray)/<span class="number">256</span>)] </span><br><span class="line">        codePic = codePic + <span class="string">&#x27;\n&#x27;</span><span class="comment">#打完一行换行</span></span><br><span class="line">    <span class="keyword">return</span> codePic</span><br><span class="line"></span><br><span class="line">text = transfrom(img)</span><br><span class="line"><span class="keyword">with</span> <span class="built_in">open</span>(<span class="string">&#x27;aa.txt&#x27;</span>,<span class="string">&#x27;w&#x27;</span>) <span class="keyword">as</span> fp:</span><br><span class="line">    fp.write(text)</span><br><span class="line"><span class="built_in">print</span>(<span class="string">&#x27;over&#x27;</span>)</span><br></pre></td></tr></table></figure>

<h3 id="键盘操作"><a href="#键盘操作" class="headerlink" title="键盘操作"></a><strong>键盘操作</strong></h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#安装pynput模块</span></span><br><span class="line">pip install pynput</span><br></pre></td></tr></table></figure>

<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#跟爬虫结合实现连发发送名人名言案例</span></span><br><span class="line"><span class="keyword">import</span> pynput <span class="keyword">as</span> p</span><br><span class="line"><span class="keyword">import</span> time</span><br><span class="line"><span class="keyword">import</span> requests</span><br><span class="line"><span class="keyword">from</span> lxml <span class="keyword">import</span> etree</span><br><span class="line"></span><br><span class="line">url = <span class="string">&#x27;http://www.yuluju.com/lizhimingyan/11246.html&#x27;</span></span><br><span class="line">headers = &#123;<span class="string">&#x27;User-Agent&#x27;</span>:<span class="string">&#x27;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36&#x27;</span>&#125;</span><br><span class="line">time.sleep(<span class="number">5</span>)</span><br><span class="line">page = requests.get(url=url,headers=headers)</span><br><span class="line">page.encoding=<span class="string">&#x27;gb2312&#x27;</span></span><br><span class="line">page_text = page.text</span><br><span class="line">tree = etree.HTML(page_text)</span><br><span class="line">span_list = tree.xpath(<span class="string">&#x27;//*[@id=&quot;p_left&quot;]/div[1]/div[4]//span/text()&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> span <span class="keyword">in</span> span_list[<span class="number">2</span>:]:</span><br><span class="line">    text = span</span><br><span class="line">    <span class="comment"># print(text)</span></span><br><span class="line">    keyboard = p.keyboard</span><br><span class="line">    key = keyboard.Key</span><br><span class="line">    ctrl = keyboard.Controller()</span><br><span class="line">    ctrl.<span class="built_in">type</span>(text)</span><br><span class="line">    time.sleep(<span class="number">1</span>)</span><br><span class="line">    ctrl.press(key.enter)</span><br><span class="line">    ctrl.release(key.enter)</span><br></pre></td></tr></table></figure>






        </div>

        
            <div class="post-copyright-info">
                <div class="article-copyright-info-container">
    <ul>
        <li>本文标题：一些Python的学习笔记</li>
        <li>本文作者：Da</li>
        <li>创建时间：2021-04-24 22:14:45</li>
        <li>
            本文链接：https://da9527.gitee.io/2021/04/24/一些Python的学习笔记/
        </li>
        <li>
            版权声明：本博客所有文章除特别声明外，均采用 <a class="license" target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh">BY-NC-SA</a> 许可协议。转载请注明出处！
        </li>
    </ul>
</div>

            </div>
        

        
            <div class="article-nav">
                
                    <div class="article-prev">
                        <a class="prev"
                           rel="prev"
                           href="/2021/04/24/markdown%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/"
                        >
                            <span class="left arrow-icon flex-center">
                              <i class="fas fa-chevron-left"></i>
                            </span>
                            <span class="title flex-center">
                                <span class="post-nav-title-item">markdown学习笔记</span>
                                <span class="post-nav-item">上一篇</span>
                            </span>
                        </a>
                    </div>
                
                
                    <div class="article-next">
                        <a class="next"
                           rel="next"
                           href="/2021/04/24/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F%E5%AD%A6%E4%B9%A0/"
                        >
                            <span class="title flex-center">
                                <span class="post-nav-title-item">正则表达式学习</span>
                                <span class="post-nav-item">下一篇</span>
                            </span>
                            <span class="right arrow-icon flex-center">
                              <i class="fas fa-chevron-right"></i>
                            </span>
                        </a>
                    </div>
                
            </div>
        

        
    </div>
</div>


                
            </div>

        </div>

        <div class="page-main-content-bottom">
            <footer class="footer">
    <div class="info-container">
        <div class="copyright-info info-item">
            &copy;
            
              <span>2020</span>&nbsp;-&nbsp;
            
            2021&nbsp;<i class="fas fa-heart icon-animate"></i>&nbsp;<a href="/">Da</a>
        </div>
        
            <script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
            <div class="website-count info-item">
                
                    <span id="busuanzi_container_site_uv">
                        访问人数&nbsp;<span id="busuanzi_value_site_uv"></span>&ensp;
                    </span>
                
                
                    <span id="busuanzi_container_site_pv">
                        总访问量&nbsp;<span id="busuanzi_value_site_pv"></span>
                    </span>
                
            </div>
        
        <div class="theme-info info-item">
            由 <a target="_blank" href="https://hexo.io">Hexo</a> 驱动&nbsp;|&nbsp;主题&nbsp;<a class="theme-version" target="_blank" href="https://github.com/XPoet/hexo-theme-keep">Keep v3.4.2</a>
        </div>
        
    </div>
</footer>

        </div>
    </div>

    
        <div class="post-tools">
            <div class="post-tools-container">
    <ul class="tools-list">
        <!-- TOC aside toggle -->
        
            <li class="tools-item page-aside-toggle">
                <i class="fas fa-outdent"></i>
            </li>
        

        <!-- go comment -->
        
    </ul>
</div>

        </div>
    

    <div class="right-bottom-side-tools">
        <div class="side-tools-container">
    <ul class="side-tools-list">
        <li class="tools-item tool-font-adjust-plus flex-center">
            <i class="fas fa-search-plus"></i>
        </li>

        <li class="tools-item tool-font-adjust-minus flex-center">
            <i class="fas fa-search-minus"></i>
        </li>

        <li class="tools-item tool-expand-width flex-center">
            <i class="fas fa-arrows-alt-h"></i>
        </li>

        <li class="tools-item tool-dark-light-toggle flex-center">
            <i class="fas fa-moon"></i>
        </li>

        <!-- rss -->
        

        
            <li class="tools-item tool-scroll-to-top flex-center">
                <i class="fas fa-arrow-up"></i>
            </li>
        

        <li class="tools-item tool-scroll-to-bottom flex-center">
            <i class="fas fa-arrow-down"></i>
        </li>
    </ul>

    <ul class="exposed-tools-list">
        <li class="tools-item tool-toggle-show flex-center">
            <i class="fas fa-cog fa-spin"></i>
        </li>
        
    </ul>
</div>

    </div>

    
        <aside class="page-aside">
            <div class="post-toc-wrap">
    <div class="post-toc">
        <ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%B8%80%E4%BA%9BPython%E7%9A%84%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0"><span class="nav-text">一些Python的学习笔记</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%8A%8Apython%E6%89%93%E5%8C%85%E6%88%90exe%E6%96%87%E4%BB%B6%E7%9A%84%E6%96%B9%E6%B3%95"><span class="nav-text">把python打包成exe文件的方法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%88%AC%E8%99%AB%E5%AD%A6%E4%B9%A0"><span class="nav-text">爬虫学习</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%AE%89%E8%A3%85%E4%B8%80%E4%BA%9B%E6%A8%A1%E5%9D%97"><span class="nav-text">安装一些模块</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#%E4%B8%80%E4%BA%9B%E7%88%AC%E8%99%AB%E6%A1%88%E4%BE%8B"><span class="nav-text">一些爬虫案例</span></a></li></ol></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%B5%8F%E8%A7%88%E5%99%A8%E8%B0%83%E8%AF%95%E6%A1%88%E4%BE%8B"><span class="nav-text">浏览器调试案例</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#%E5%85%88%E4%B8%8B%E8%BD%BD%E6%B5%8F%E8%A7%88%E5%99%A8%E9%A9%B1%E5%8A%A8-%E8%BF%99%E9%87%8C%E6%98%AFChrome%E9%A9%B1%E5%8A%A8-ChromeDriver-Mirror-taobao-org-%EF%BC%8C%E5%AE%89%E8%A3%85selenium"><span class="nav-text">先下载浏览器驱动,这里是Chrome驱动(ChromeDriver Mirror (taobao.org))，安装selenium</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#%E4%B8%8B%E9%9D%A2%E6%98%AFchrome%E8%B7%9F%E7%81%AB%E7%8B%90%E7%9A%84%E6%A1%88%E4%BE%8B"><span class="nav-text">下面是chrome跟火狐的案例</span></a></li></ol></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%97%A0%E5%A4%B4%E6%B5%8F%E8%A7%88%E5%99%A8%E8%B0%83%E8%AF%95"><span class="nav-text">无头浏览器调试</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%8E%B7%E5%8F%96%E7%99%BE%E5%BA%A6%E7%BF%BB%E8%AF%91%E5%86%85%E5%AE%B9"><span class="nav-text">获取百度翻译内容</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%94%A8python%E7%94%BB%E5%9B%BE"><span class="nav-text">用python画图</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#%E7%94%BB%E5%9B%BE%E6%A8%A1%E5%9D%97%E4%B8%8B%E8%BD%BDturtle-https-files-pythonhosted-org-packages-ff-f0-21a42e9e424d24bdd0e509d5ed3c7dfb8f47d962d9c044dba903b0b4a26f-turtle-0-0-2-tar-gz"><span class="nav-text">画图模块下载turtle(https:&#x2F;&#x2F;files.pythonhosted.org&#x2F;packages&#x2F;ff&#x2F;f0&#x2F;21a42e9e424d24bdd0e509d5ed3c7dfb8f47d962d9c044dba903b0b4a26f&#x2F;turtle-0.0.2.tar.gz)</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#%E6%89%93%E5%BC%80setup-py%E6%96%87%E4%BB%B6%EF%BC%8C%E7%AC%AC40%E8%A1%8C%E4%BF%AE%E6%94%B9%E4%B8%BA"><span class="nav-text">打开setup.py文件，第40行修改为 :</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#%E7%94%BB%E5%9B%BE%E6%A1%88%E4%BE%8B%EF%BC%9A"><span class="nav-text">画图案例：</span></a></li></ol></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%9B%BE%E7%89%87%E8%BD%AC%E5%AD%97%E7%AC%A6%E7%94%BB"><span class="nav-text">图片转字符画</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%94%AE%E7%9B%98%E6%93%8D%E4%BD%9C"><span class="nav-text">键盘操作</span></a></li></ol></li></ol>
    </div>
</div>
        </aside>
    

    <div class="image-viewer-container">
    <img src="">
</div>


    
        <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
          <span class="search-input-field-pre">
            <i class="fas fa-keyboard"></i>
          </span>
            <div class="search-input-container">
                <input autocomplete="off"
                       autocorrect="off"
                       autocapitalize="off"
                       placeholder="搜索..."
                       spellcheck="false"
                       type="search"
                       class="search-input"
                >
            </div>
            <span class="popup-btn-close">
                <i class="fas fa-times"></i>
            </span>
        </div>
        <div id="search-result">
            <div id="no-result">
                <i class="fas fa-spinner fa-pulse fa-5x fa-fw"></i>
            </div>
        </div>
    </div>
</div>

    

</main>




<script src="/js/utils.js"></script>

<script src="/js/main.js"></script>

<script src="/js/header-shrink.js"></script>

<script src="/js/back2top.js"></script>

<script src="/js/dark-light-toggle.js"></script>



    
<script src="/js/local-search.js"></script>




    
<script src="/js/code-copy.js"></script>




    
<script src="/js/lazyload.js"></script>



<div class="post-scripts pjax">
    
        
<script src="/js/left-side-toggle.js"></script>

<script src="/js/libs/anime.min.js"></script>

<script src="/js/toc.js"></script>

    
</div>


    
<script src="/js/libs/pjax.min.js"></script>

<script>
    window.addEventListener('DOMContentLoaded', () => {
        const pjax = new Pjax({
            selectors: [
                'head title',
                '.page-container',
                '.pjax'
            ],
            history: true,
            debug: false,
            cacheBust: false,
            timeout: 0,
            analytics: false,
            currentUrlFullReload: false,
            scrollRestoration: false,
            // scrollTo: true,
        });

        document.addEventListener('pjax:send', () => {
            KEEP.utils.pjaxProgressBarStart();
        });

        document.addEventListener('pjax:complete', () => {
            KEEP.utils.pjaxProgressBarEnd();
            pjax.executeScripts(document.querySelectorAll('script[data-pjax], .pjax script'));
            KEEP.refresh();
        });
    });
</script>



<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"debug":false,"model":{"jsonPath":"/live2dw/assets/shizuku.model.json"},"display":{"position":"left","width":120,"height":240},"mobile":{"show":false},"log":false});</script></body>
</html>
